package com.litxuf.logic.admin.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.litxuf.logic.admin.dao.model.UserAdmin;

public class UserAdminDAO extends HibernateDaoSupport {

	public UserAdmin findByUserNameAndPwd(String userName, String pwd) {
		 String sql="FROM UserAdmin t where t.username='"+userName+"' and t.password='"+pwd+"'";
		 List list=this.getHibernateTemplate().find(sql);
		 if(list.size()==0){
			 return null;
		 }else{
			 return (UserAdmin)list.get(0);
		 }
	}

	public String save(UserAdmin info) {
		String pk = null;
		try {
			pk = getHibernateTemplate().save(info).toString();
		} catch (Exception e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		}
		return pk;
	}

	public void update(UserAdmin obj) {
		this.getHibernateTemplate().update(obj);
	}

	public UserAdmin findByUserId(int userId) {
		return (UserAdmin)this.getHibernateTemplate().get(UserAdmin.class, userId);
	}
	
	
	//SiteConfig
	
	public List findByUserName(String username) {
		String sql = "FROM UserAdmin t where t.username='" + username + "'";
		List list = this.getHibernateTemplate().find(sql);
		return list;
	}

	/**
	 * 所有的普通操作员列表 made in 2010-1-5下午10:47:47
	 * 
	 * @return
	 * 
	 */
	public List findAllBasicUser() {
		String sql = "FROM UserAdmin t where t.userlevel=1";
		List list = this.getHibernateTemplate().find(sql);
		return list;
	}

	/**
	 * 删除用户 made in 2010-1-5下午10:53:25
	 * 
	 * @param id
	 * 
	 */
	public void deleUser(final int id) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				String hql = "delete from UserAdmin t where t.userId= " + id;
				Query query = session.createQuery(hql);
				return query.executeUpdate();
			}

		});

	}

}
